package com.yoomiito.app.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.gift.YouPinAdapter;
import com.yoomiito.app.model.gift.GiftGoods;
import com.yoomiito.app.model.gift.GiftGoodsList;
import com.yoomiito.app.model.gift.GiftType;
import com.yoomiito.app.ui.order.sureorder.shopcar.ShopCarActivity;
import com.yoomiito.app.present.YouPinGoodsPresenter;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.widget.TabView;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.StatusManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 优品 爆品  自营商品
 */
public class YouPinGoodsActivity extends BaseActivity<YouPinGoodsPresenter> {
    public static final String YouPinGoodsAct = "you_pin_goods_act";
    @BindView(R.id.tv_center)
    TextView mTitleTv;
    @BindView(R.id.act_youpin_goods_tabView)
    TabView mTabView;
    @BindView(R.id.frameLayout)
    FrameLayout mFrameLayout;
    SmartRefreshLayout mRefreshLayout;
    RecyclerView mRecyclerView;

    private int mCurrentPage = 1;
    private String mSort = "";
    private long mType = 0;
    private int mGoodsType = 3; //默认优品
    private YouPinAdapter giftAdapter;
    private List<GiftGoods> mGiftGoods;
    private List<GiftType> mGiftTypes;
    private int goodsType;
    private View mShopCarShowNew;

    @Override
    public int getLayoutId() {
        return R.layout.act_youpin_goods;
    }

    @Override
    public YouPinGoodsPresenter newP() {
        return new YouPinGoodsPresenter(App.mAppConfig);
    }

    public static void start(Context context, String title, int type) {
        Intent intent = new Intent(context, YouPinGoodsActivity.class);
        intent.putExtra(Constant.DATA, title);
        intent.putExtra(Constant.TYPE, type);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (goodsType == AllCode.BaoPin&&App.mAppConfig.getIsLogin()) {
            getP().getList(1);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mStatusManager = new StatusManager.Builder(this)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new EmptyType().setEmptyContent("暂无商品"))
                .build();
        mRefreshLayout = mStatusManager.view().findViewById(R.id.fm_refreshLayout);
        mRecyclerView = mStatusManager.view().findViewById(R.id.fm_rcy);
        mFrameLayout.addView(mStatusManager.view());
        mTitleTv.setVisibility(View.VISIBLE);
        String title = getIntent().getStringExtra(Constant.DATA);
        goodsType = getIntent().getIntExtra(Constant.TYPE, AllCode.YouPin);
        switch (goodsType) {
            case AllCode.BaoPin:
                mGoodsType = 2;
//                if (App.mAppConfig.getIsLogin()) {
                View view = LayoutInflater.from(context).inflate(R.layout.icon_shop_car, null);
                mShopCarShowNew = view.findViewById(R.id.show_new);
                view.setOnClickListener(v -> {
                    if (App.mAppConfig.getIsLogin()){
                        ShopCarActivity.start(YouPinGoodsActivity.this);
                    }else {
//                        AllUtils.toLogin(YouPinGoodsActivity.this,AllCode.LoginIn);
                        LoginUtils.INSTANCE.toOneKeyLogin(YouPinGoodsActivity.this);
                    }
                });
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(DensityUtil.dp2px(48), DensityUtil.dp2px(48));
                lp.bottomMargin = DensityUtil.dp2px(12);
                lp.rightMargin = DensityUtil.dp2px(12);
                lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
                mFrameLayout.addView(view, lp);
//                }
                break;
            case AllCode.YouPin:
                mGoodsType = 3;
                break;
            case AllCode.AgentMall:
                mGoodsType = 4;
                break;
        }
        mTitleTv.setText(title);
        initTabView();
        initRcy();
        initRefreshLayout();
        getP().getGiftType(mGoodsType);
    }

    private void initTabView() {
        mTabView.setDownName("_des")
                .setUpName("_asc")
                .setPriceFirstName("sale_price")
                .setSaleFirstName("volume")
                .showSort()
                .setDefault();
//        mTabView.setOnChangeListener((sort, hasCoupon) -> {
////            mSort = sort;
//            mCurrentPage = 1;
//            getP().getVipGiftGoods(mSort, mCurrentPage, mType, mGoodsType);
//        });

        mTabView.setOnSortClickListener(giftType -> {
                    mCurrentPage = 1;
                    mType = giftType.getId();
                    showLoading();
                    getP().getVipGiftGoods(mSort, mCurrentPage, mType, mGoodsType);
                }

        );
    }

    private void initRefreshLayout() {
        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            mCurrentPage = 1;
            getP().getVipGiftGoods(mSort, mCurrentPage, mType, mGoodsType);
        });
        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mCurrentPage++;
            getP().getVipGiftGoods(mSort, mCurrentPage, mType, mGoodsType);
        });
        mRefreshLayout.autoRefresh();
    }

    private void initRcy() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mGiftGoods = new ArrayList();
        giftAdapter = new YouPinAdapter(mGiftGoods, goodsType);
        mRecyclerView.setAdapter(giftAdapter);
        giftAdapter.setOnItemClickListener((adapter, view, position) ->
                RouterUtils.toGoodsDetail(YouPinGoodsActivity.this, String.valueOf(giftAdapter.getData().get(position).getId()), goodsType)
        );
    }

    public void showGift(GiftGoodsList goodsList) {
        List<GiftGoods> data = goodsList.getList().getData();
        if (mCurrentPage == 1) {
            if (data == null || data.size() == 0) {
                mStatusManager.showEmpty();
            } else {
                mStatusManager.showContent();
                giftAdapter.setNewData(data);
            }
            mRefreshLayout.finishRefresh(true);
            mRefreshLayout.setNoMoreData(false);
        } else {
            mRefreshLayout.finishLoadMore(true);
            if (data == null || data.size() == 0) {
                Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show();
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                giftAdapter.addData(data);
            }
        }
    }

    public void setType(List<GiftType> types) {
        if (types != null) {
            mGiftTypes = types;
            mGiftTypes.add(0, new GiftType(0, "全部", true));
            mTabView.setGiftTypes(mGiftTypes);
        }
    }

    public void showNewFlag(boolean show) {
        mShopCarShowNew.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @OnClick({R.id.iv_back_left})
    public void onClick(View view) {
        finish();
    }

    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);
        mRefreshLayout.finishLoadMore(true);
    }
}
